/**
 * 所有版权归 广西梧州 陈锦韬(身份证后2位36) 所有
 *
 * @BelongsProject: ismartback
 * @BelongsPackage: com.rx.mylist.service
 * @Author: 陈锦韬
 * @CreateTime: 2023-02-25  12:52
 * @Description: TODO
 * @Version: 1.0
 * @Copyright: 2023 陈锦韬 All rights reserved.
 */
package com.rx.mylist.service;

import cn.binarywang.wx.miniapp.api.WxMaQrcodeService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaCodeLineColor;
import com.rx.core.enm.HANDLER_TYPE;
import com.rx.core.enm.RESULT_CODE;
import com.rx.core.service.RxBaseAbstractService;
import com.rx.core.service.StandardService;
import com.rx.core.util.EmptyChecker;
import com.rx.core.util.RxUtil;
import com.rx.mylist.bean.WxConfigurations;
import com.rx.mylist.dao.ListEssayInfoMapper;
import com.rx.mylist.vo.ListGuardInfo;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.util.Arrays;

public class ListBothService extends RxBaseAbstractService {

    protected static final String WX = "weixin";


    protected static final String OWNER_LIMIT = "1";
    protected static final String OWNER_ALL = "0";
    protected static final String OWNER_APPLY = "2";
    protected static final String SHOW_SQUARE = "1";

    protected static final String WY_NO_QUERY = "1";
    protected static final String NEED_CHECK = "1";
    protected static final String HAVE_CHECK = "2";

    protected static final String EN_KEY = "120394856";
    protected static final String EN_BACK_PRE = "BACK";

    protected static final String HIDE_KEY = "01";
    protected static final String HIDE_MANA_KEY = "02";
    protected static final String HIDE_PROPERTY_KEY = "03";

    protected static final String COMPANY_QR_TYPE="02";

    @Autowired
    protected WxConfigurations wxConfiguration;
    @Autowired
    protected StandardService standardService;

    @Autowired
    protected ListCacheService cacheService;

    @Autowired
    protected ListEssayInfoMapper essayInfoMapper;

    protected Object createQr(ListGuardInfo guardInfo) throws IllegalAccessException, InstantiationException {


        if(EmptyChecker.isEmpty(guardInfo.getBid())){
            // 新增获取二维码
            WxMaService wxMaService =  ((WxMaService) wxConfiguration.wxService(wxConfiguration.wxMaConfig()));
            WxMaQrcodeService wxMaQrcodeService = wxConfiguration.wxService(wxMaService);

            try {
                File file= wxMaQrcodeService.createWxaCodeUnlimit(guardInfo.getScene(),"pages/tab/home/index",false,"release",
                        298,false, new WxMaCodeLineColor("255","0","0"),false);

                guardInfo.setImage("data:image/PNG;BASE64,"+ RxUtil.FileUtil.fileToBase64(file));

            } catch (WxErrorException e) {
                RxUtil.CheckUtil.throwOut(RESULT_CODE.MESSAGE,"生成二维码失败！");
            }
        }

        ListGuardInfo listGuardInfo = (ListGuardInfo) ListGuardInfo.create(ListGuardInfo.class, HANDLER_TYPE.SINGLE_BATCH_SAVE);
        listGuardInfo.setDataList(Arrays.asList(guardInfo));
        return standardService.batchSave(listGuardInfo);
    }
}
